<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_worker_ewm/model')->方法名();
*/
namespace jy_worker_ewm;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	public function uplog($listid=0){
		$data=array(
			'listid'=>$listid,
			'mobile'=>$_SESSION['member']['mobile'],
			'nickname'=>$_SESSION['member']['nickname'],
		);
		$nowlistid=pdo_getcolumn('jy_worker_ewm_log',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'listid');
		if(!$nowlistid){
			$data['uniacid']=$_SESSION['uniacid'];
			$data['mid']=$_SESSION['uid'];
			$data['merchid']=$_SESSION['onemerchid'];
			$data['username']=$_SESSION['member']['username'];
			$data['createtime']=time();
			pdo_insert('jy_worker_ewm_log',$data);
		}else{
			pdo_update('jy_worker_ewm_log',$data,array('merchid'=>$_SESSION['onemerchid'],'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		}
		return true;
	}
	public function checkgo($listid=0){
		if(!$_SESSION['member']['nickname']){
			return true;
		}
		$isadd=pdo_get('jy_scrm_member',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
		if(!$isadd){
			$isadd=pdo_get('jy_scrm_member',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'name'=>$_SESSION['member']['nickname']));
		}
		if(!$isadd){
			$isadd=pdo_get('jy_scrm_member',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'nickname'=>$_SESSION['member']['nickname']));
		}
		if($isadd){
			$list=pdo_get('jy_worker_ewm_list',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'id'=>$listid));
			if($list['link']){
				exi('','',$list['link']);
			}
		}
		return true;
	}
	public function jie_state($state,$ExternalUserID='',$UserID=''){
		$huoma=pdo_get('jy_worker_ewm_list',array('uniacid'=>$_SESSION['uniacid'],'id'=>$state[2]));
		$worker=pdo_get('jy_worker_ewm_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$huoma['workerid']));
		$_SESSION['onemerchid']=$huoma['merchid'];
		$member=pdo_get('jy_scrm_member',array('merchid'=>$_SESSION['onemerchid'],'external_userid'=>$ExternalUserID,'uniacid'=>$_SESSION['uniacid']),array('mid','name'));
		if($worker['shoplevel_id'] && $member['mid']){
			$user=pdo_get('jy_weishop_member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$member['mid'],'uniacid'=>$_SESSION['uniacid']),array('id'));
			if(!$user['id']){
				$insert=array(
					'mid'=>$member['mid'],
					'merchid'=>$_SESSION['onemerchid'],
				);
				model('jy_weishop/member')->register($insert);
			}
			model('jy_weishop/member')->up_member_level($member['mid'],$worker['shoplevel_id'],$_SESSION['onemerchid']);
		}
		$basic=$this->plugin_setting('basic');
		$attachments=array();
		if($worker['welcomeid']){
				$username=$member['name'];
				$username=$username?:'无昵称';
				$welcome=pdo_get('jy_scrm_welcome',array('uniacid'=>$_SESSION['uniacid'],'id'=>$worker['welcomeid']));
				$welcome['image']=$welcome['image']?json_decode($welcome['image'],true):array();
				$welcome['link']=$welcome['link']?json_decode($welcome['link'],true):array();
				$welcome['miniprogram']=$welcome['miniprogram']?json_decode($welcome['miniprogram'],true):array();
				$welcome['video']=$welcome['video']?json_decode($welcome['video'],true):array();
				$welcome['file']=$welcome['file']?json_decode($welcome['file'],true):array();
				//if($welcome['content']==urlencode(urldecode($welcome['content']))){
					$welcome['content']=urldecode($welcome['content']);
				//}
				$welcome['content']=str_replace('[客户名称]',$username,$welcome['content']);
				if($welcome['miniprogram']['title']){
					$attachments[]=array(
						'msgtype'=>'miniprogram',
						'miniprogram'=>array(
							'title'=>$welcome['miniprogram']['title'],
							'pic_media_id'=>$welcome['miniprogram']['pic_media_id'],
							'appid'=>$welcome['miniprogram']['appid'],
							'page'=>$welcome['miniprogram']['path'],
						),
					);
				}
				if($welcome['image']['media_id']){
					$attachments[]=array(
						'msgtype'=>'image',
						'image'=>array(
							'media_id'=>$welcome['image']['media_id'],
						),
					);
				}
				if($welcome['video']['media_id']){
					$attachments[]=array(
						'msgtype'=>'video',
						'video'=>array(
							'media_id'=>$welcome['video']['media_id'],
						),
					);
				}
				if($welcome['link']['title']){
					$attachments[]=array(
						'msgtype'=>'link',
						'link'=>array(
							'title'=>$welcome['link']['title'],
							'picurl'=>tomedia($welcome['link']['picurl']),
							'desc'=>$welcome['link']['desc'],
							'url'=>$welcome['link']['url'],
						),
					);
				}
		}
		$huoma['linkimg']=$huoma['linkimg']?:$worker['linkimg'];
		if($basic['wxapp_id']){
			$wxapp=pdo_get('core_account_wxapp',array('id'=>$basic['wxapp_id'],'uniacid'=>$_SESSION['uniacid']));
			$attachments[]=array(
				'msgtype'=>'miniprogram',
				'miniprogram'=>array(
					'title'=>$huoma['linktitle']?:$worker['linktitle'],
					'picurl'=>tomedia($huoma['linkimg']),
					'appid'=>$wxapp['appid'],
					'page'=>'jy_worker_ewm/golink/index?merchid='.$_SESSION['onemerchid'].'&id='.$huoma['id'],
				),
			);
		}else{
			$attachments[]=array(
				'msgtype'=>'link',
				'link'=>array(
					'title'=>$huoma['linktitle']?:$worker['linktitle'],
					'picurl'=>tomedia($huoma['linkimg']),
					'desc'=>$huoma['linkdesc']?:$worker['linkdesc'],
					'url'=>custom_url('jy_worker_ewm/golink//merchid='.$_SESSION['onemerchid'].'&id='.$huoma['id'],true),
				),
			);
		}
		$msg=array(
			'text'=>array(
				'content'=>$welcome['content'],
			),
			'attachments'=>$attachments,
		);
		return $msg;
	}
}
?>